package com.hidt.shop.controller;

import com.hidt.shop.domain.Collection;
import com.hidt.shop.domain.Goods;
import com.hidt.shop.service.CollectionService;
import org.apache.ibatis.scripting.xmltags.ForEachSqlNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class CollectionController {
    @Autowired
    private CollectionService collectionService;
    /**
     * 通过用户id查看收藏信息判断该商品是否收藏过
     * 查询到显示已收藏，未查询到显示收藏
     *@参数  collection
     *@返回值  int
     *@创建人  lmf
     *@创建时间  2021/1/20
     */
    @RequestMapping("/findcollection")
    @ResponseBody
    public Map FindCollectionByUserid(@RequestBody Collection collection){
        int i = collectionService.FindCollection(collection);
        System.out.println(collection);
        HashMap map=new HashMap();
        if(i==1){
            map.put("mes","已收藏");
            System.out.println(1111);
        }else {
            map.put("mes","未收藏");
            System.out.println(2222);
        }
        return map;
    }
    /**
     * 添加收藏
     *@参数  collection
     *@返回值  int
     *@创建人  lmf
     *@创建时间  2021/1/20
     */
    @RequestMapping("/addcollection")
    @ResponseBody
    public Map AddCollection(@RequestBody Collection collection){
        System.out.println(collection);
        int i = collectionService.AddCollection(collection);
        HashMap map=new HashMap();
        if (i>0){
           map.put("mes","收藏成功");
        }else {
            map.put("mes","收藏失败");
        }
        return map;
    }

    /**
     * 通过id查看收藏
     *@参数  id
     *@返回值  List<Collection>
     *@创建人  lmf
     *@创建时间  2021/1/20
     */
    @RequestMapping("/findallcollection")
    @ResponseBody
    public Map FindAllCollection(@RequestBody Map<String,String> map){
        int id = Integer.parseInt(map.get("id"));
        List collectList = collectionService.FindAllCollection(id);
//        System.out.println(collectList);
//        for (Object list1:collectList) {
//            System.out.println(list1);
//        }
        HashMap map1 = new HashMap();
        map1.put("collectList",collectList);
        map1.put("mes","查看收藏");
        return map1;
    }

    /**
     * 通过id删除收藏
     *@参数  collection
     *@返回值  String
     *@创建人  lmf
     *@创建时间  2021/1/20
     */
    @RequestMapping("/deletecollection")
    @ResponseBody
    public Map DeleteCollertion(@RequestBody Collection collection){
        System.out.println(collection);
        int i = collectionService.DeleteCollection(collection);
        HashMap map=new HashMap();
        if(i>0){
            map.put("mes","删除成功");
        }else {
            map.put("mes","删除失败");
        }
        return map;
    }
}
